<?php defined('SYSPATH') or die('No direct script access.');

class Model_Finance_Listener extends ORM
{
    /**
     * @param $order_sn
     * @return array|bool
     * 预订产品设置记录相应的成本和基础价
     */
    public static function order_set_count_info($order_sn)
    {
        try {
            if(!$order_sn){
                throw new Exception("订单号不存在");
            }
            $order = DB::select()->from('member_order')->where('ordersn','=',$order_sn)->execute()->current();
            if(!$order){
                throw new Exception("订单不存在");
            }
            $res = Model_Member_Order_Extend::add_order_extend($order);
        } catch (Exception $e) {
            $res = [
                'status' => true,
                'msg'    => $e->getMessage()
            ];
        }

        return $res;
    }

    /**
     * @param $order_sn
     * @return array
     * 订单付款成功后调用计算佣金(废弃,在下单时就已经计算)
     */
    public static function order_caculate_platform_commission($order_sn)
    {
        try {
            if(!$order_sn){
                throw new Exception("订单号不存在");
            }
            $order = DB::select()->from('member_order')->where('ordersn','=',$order_sn)->execute()->current();
            if(!$order){
                throw new Exception("订单不存在");
            }
            $res = Model_Member_Order_Extend::add_commission_record($order);
        } catch (Exception $e) {
            $res = [
                'status' => true,
                'msg'    => $e->getMessage()
            ];
        }

        return $res;
    }

    public static function platform_income($order_sn)
    {
        try {
            $order = Model_Member_Order::order_info($order_sn);
            if (! $order) {
                throw new Exception('订单不存在');
            }
            if (!in_array($order['status'],array(2,5))) {
                throw new Exception('非已支付状态');
            }
            if ($order['payprice'] <= 0) {
                throw new Exception('支付金额小于或等于0');
            }

            $model = Model_Model::get_module_info($order['typeid']);
            //在线支付则自动确认状态.
            $status = $order['online_transaction_no'] ? 1 : 0;
            $arr = array(
                'ordersn'   => $order['ordersn'],
                'type_id'   => $order['typeid'],
                'amount'    => $order['payprice'],
                'mid'       => $order['memberid'],
                'pdt_type'  => $model['modulename'],
                'pdt_name'  => $order['productname'],
                'pay_time'  => $order['paytime'],
                'pay_type'  => $order['paysource'],
                'pay_num'   => $order['online_transaction_no'],
                'pay_proof' => $order['payment_proof'],
                'salesman'  => $order['saleman'],
                'linkman'   => $order['linkman'],
                'linktel'   => $order['linktel'],
                'status'    => $status,
            );

            //记账特殊处理
            if($order['typeid']==99){
                $account_order = DB::select()
                    ->from('accounting_order')
                    ->where('order_id','=',$order['id'])
                    ->execute()
                    ->current();
                $type = $account_order['account_type'];
                $text = $type==1?"订单收入":"订单支出";
                Model_Finance_Record::add_finance_record($arr['amount'],$type,$text,'订单'.$arr['ordersn'].'支付成功'.$arr['amount'],$arr['ordersn']);
            }else{
                //收支明细记录添加
                Model_Finance_Record::add_finance_record($arr['amount'],1,'订单收入','订单'.$arr['ordersn'].'支付成功'.$arr['amount'],$arr['ordersn']);
            }


            if (!Model_Finance_Income::save_info($arr)) {
                throw new Exception('添加收入记录失败');
            }
            $res = [
                'status' => true,
                'msg'    => '已添加收入记录'
            ];
        } catch (Exception $e) {
            $res = [
                'status' => false,
                'msg'    => $e->getMessage()
            ];
        }

        return $res;
    }

}